package com.yuke.cloud.service.omc.controller.frontend;

import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.core.config.RedisSender;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.RedisKeyUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillDto;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillSkuDto;
import com.yuke.cloud.service.omc.dto.SecKillOrderDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yukemall </br> </p>
 * <p>com.yuke.cloud.service.omc.controller.frontend Description TODO </br> </p>
 * @Author wg
 * @Date   2019/6/5 10:24
 */
@Api(value = "WEB - OdsPromoteGoodsController", tags = "处理商品促销活动相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/promoteGoods")
@Import({RedisSender.class})
public class OdsPromoteGoodsController extends BaseController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisSender redisSender;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "odsShopCart", value = "该数据记录的POJO") @RequestBody SecKillOrderDto secKillOrderDto) {

        // 判断是否可以抢购
        CmsSecKillDto cmsSecKillDto = (CmsSecKillDto)redisTemplate.opsForValue().get(RedisKeyUtil.getSecKillKey(String.valueOf(secKillOrderDto.getSecKillId())));
        CmsSecKillSkuDto cmsSecKillSkuDto = (CmsSecKillSkuDto)redisTemplate.opsForValue().get(RedisKeyUtil.getSecKillSkuKey(String.valueOf(secKillOrderDto.getSecKillId()), String.valueOf(secKillOrderDto.getSpecId())));
        int boughtCnt = 0;
        if (!redisTemplate.hasKey(RedisKeyUtil.getSecKillSkuUserBuy(String.valueOf(secKillOrderDto.getSecKillId()),
                String.valueOf(secKillOrderDto.getSpecId()), String.valueOf(secKillOrderDto.getUserId())))) {
            boughtCnt = (int)redisTemplate.opsForValue().get(RedisKeyUtil.getSecKillSkuUserBuy(String.valueOf(secKillOrderDto.getSecKillId()),
                    String.valueOf(secKillOrderDto.getSpecId()), String.valueOf(secKillOrderDto.getUserId())));
        }

        int i = 0; // 0：正常，1：活动未开始或结束，2：商品已售完或不存在，3：超过购买数量
        if (cmsSecKillDto == null) {
            i = 1;
        } else {
            Date now = new Date();
            if (now.getTime() < cmsSecKillDto.getStartTime().getTime() || now.getTime() > cmsSecKillDto.getEndTime().getTime()) {
                i = 1;
            }
        }

        if (cmsSecKillSkuDto == null) {
            i = 2;
        } else {
            if (boughtCnt + secKillOrderDto.getNum() > cmsSecKillSkuDto.getMaxBuyCount()) {
                i = 3;
            }

            if (cmsSecKillSkuDto.getStoreCount() - cmsSecKillSkuDto.getLockCount() < secKillOrderDto.getNum()) {
                i = 2;
            }
        }

        if (i == 0) {
            redisSender.sendChannelMess(GlobalConstant.SEC_KILL_REDIS_TOPIC, JSONObject.toJSONString(secKillOrderDto));

            return WrapMapper.ok();
        } else if (i == 1) {
            return WrapMapper.error("促销活动未开始或已结束");
        } else if (i == 2) {
            return WrapMapper.error("促销商品已售完或不存在");
        } else if (i == 3) {
            return WrapMapper.error("您已超过该促销商品购买限制数量");
        }

        return WrapMapper.ok();
    }
}
